home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / PSPLIT.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  98 lines

  1. /*
  2. **  psplit() - Portable replacement for fnsplit(), _splitpath(), etc.
  3. **
  4. **  Splits a full DOS pathname into drive, path, file, and extension
  5. **  specifications. Works with forward or back slash path separators and
  6. **  network file names, e.g. NET:LOONEY/BIN\WUMPUS.COM, Z:\MYDIR.NEW/NAME.EXT
  7. **
  8. **  Arguments: 1 - Full pathname to split
  9. **             2 - Buffer for drive
  10. **             3 - Buffer for path
  11. **             4 - Buffer for name
  12. **             5 - Buffer for extension
  13. **
  14. **  Returns: Nothing
  15. **
  16. **  public domain by Bob Stout
  17. */
  18.  
  19. #include <stdlib.h>
  20. #include <string.h>
  21.  
  22. #define NUL '\0'
  23.  
  24. void psplit(char *path, char *drv, char *dir, char *fname, char *ext)
  25. {
  26.       char ch, *ptr, *p;
  27.  
  28.       /* convert slashes to backslashes for searching       */
  29.  
  30.       for (ptr = path; *ptr; ++ptr)
  31.       {
  32.             if ('/' == *ptr)
  33.                   *ptr = '\\';
  34.       }
  35.  
  36.       /* look for drive spec                                */
  37.  
  38.       if (NULL != (ptr = strchr(path, ':')))
  39.       {
  40.             strncpy(drv, path, ++ptr - path);
  41.             drv[ptr - path] = NUL;
  42.             path = ptr;
  43.       }
  44.       else  *drv = NUL;
  45.  
  46.       /* find rightmost backslash or leftmost colon         */
  47.  
  48.       if (NULL == (ptr = strrchr(path, '\\')))
  49.             ptr = (strchr(path, ':'));
  50.  
  51.       if (!ptr)
  52.       {
  53.             ptr = path;             /* obviously, no path   */
  54.             *dir = NUL;
  55.       }
  56.       else
  57.       {
  58.             ++ptr;                  /* skip the delimiter   */
  59.             ch = *ptr;
  60.             *ptr = NUL;
  61.             strcpy(dir, path);
  62.             *ptr = ch;
  63.       }
  64.  
  65.       if (NULL == (p = strrchr(ptr, '.')))
  66.       {
  67.             strcpy(fname, ptr);
  68.             *ext = NUL;
  69.       }
  70.       else
  71.       {
  72.             *p = NUL;
  73.             strcpy(fname, ptr);
  74.             *p = '.';
  75.             strcpy(ext, p);
  76.       }
  77. }
  78.  
  79. #ifdef TEST
  80.  
  81. #include <stdio.h>
  82.  
  83. int main(int argc, char *argv[])
  84. {
  85.       char drive[10], pathname[FILENAME_MAX], fname[9], ext[5];
  86.  
  87.       while (--argc)
  88.       {
  89.             psplit(*++argv, drive, pathname, fname, ext);
  90.             printf("psplit(%s) returns:\n drive = %s\n path  = %s\n"
  91.                   " name  = %s\n ext   = %s\n",
  92.                   *argv, drive, pathname, fname, ext);
  93.       }
  94.       return EXIT_SUCCESS;
  95. }
  96.  
  97. #endif
  98.